//
//  WDCheckController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/2/22.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDCheckController.h"
#import "WDLoanDetailController.h"
#import "WDLoan.h"
#import "WDCheckHeadView.h"
#import "WDCheckCell.h"
#import "WDCheck.h"
#import "WDMaterialList.h"
#import "WDOriImageController.h"

@interface WDCheckController ()<UITableViewDelegate, UITableViewDataSource, checkCellDelegate>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 审核数据 */
@property (nonatomic, strong) NSArray *checkArray;
/** 状态图片 */
@property (nonatomic, strong) NSArray *imagesArray;

@end

@implementation WDCheckController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.isNotify) {
        [self loadNavItemWithLeftAction:@selector(clickBackBtn) leftBtnImage:@"icon_back" leftBtnSelImage:@"icon_back" title:@"查看状态" rihgtAction:@selector(clickDetailBtn) rightBtnImage:@"icon_detail" rightBtnSelImage:@"icon_detail"];
    } else {
        
        [self loadNavItemWithAction:@selector(clickBackBtn) btnImage:@"icon_back" btnSelImage:@"icon_back" title:@"查看状态"];
        
    }
    
    [self requestLoanData];
    
    [self requestGetHis];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    
    // 设置导航栏背景为透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault];
    
    // 隐藏导航栏底部黑线
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 当都设置为nil的时候，导航栏会使用默认的样式，即还原导航栏样式
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = nil;
}

#pragma mark - 加载控件
- (void)loadTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, WDScreenH - 64) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.bounces = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

- (void)clickBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickDetailBtn
{
    //查看详情
    if ([self.loan.status intValue] == WDLoanTypeDelay || [self.loan.status intValue] == WDLoanTypeNotPass || [self.loan.status intValue] == WDLoanTypeIDNotPass) {
        WDLoanDetailController *tempVC = [[WDLoanDetailController alloc] init];
        tempVC.loan = self.loan;
        tempVC.isCheck = YES;
        tempVC.hiddenBtn = NO;
        tempVC.view.backgroundColor = WDBackColor;
        
        [self.navigationController pushViewController:tempVC animated:YES];
    } else {
        WDLoanDetailController *tempVC = [[WDLoanDetailController alloc] init];
        tempVC.loan = self.loan;
        tempVC.isCheck = YES;
        tempVC.hiddenBtn = YES;
        tempVC.view.backgroundColor = WDBackColor;
        
        [self.navigationController pushViewController:tempVC animated:YES];
    }
}

#pragma mark - 请求数据
- (void)requestLoanData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    
    manager.requestSerializer.timeoutInterval = 10.0f;
    
    NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:WDURLLogin];
    if([cookiesdata length]) {
        NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
        NSHTTPCookie *cookie;
        for (cookie in cookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }
    }
    
    NSDictionary *parameters = @{
                                 @"finance_id":self.financeID,
                                 @"ctype":@3
                                 };
    NSString *url = [NSString stringWithFormat:@"%@%@", WDURLPrefix, WDURLFinanceDetail];
    
    [manager POST:url parameters:parameters constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        WDLoan *loan = [[WDLoan alloc] init];
        [loan setValuesForKeysWithDictionary:responseObject];
        
        self.loan = loan;
        
        if ([loan.brand_id isKindOfClass:[NSNumber class]]) {
            loan.brand_id = [NSString stringWithFormat:@"%@", loan.brand_id];
        }
        
        if ([loan.car_price isKindOfClass:[NSNumber class]]) {
            loan.car_price = [NSString stringWithFormat:@"%@", loan.car_price];
        }
        
        if ([loan.car_type isKindOfClass:[NSNumber class]]) {
            loan.car_type = [NSString stringWithFormat:@"%@", loan.car_type];
        }
        
        if ([loan.id isKindOfClass:[NSNumber class]]) {
            loan.id = [NSString stringWithFormat:@"%@", loan.id];
        }
        
        if ([loan.is_throw isKindOfClass:[NSNumber class]]) {
            loan.is_throw = [NSString stringWithFormat:@"%@", loan.is_throw];
        }
        
        if ([loan.loan_amount isKindOfClass:[NSNumber class]]) {
            loan.loan_amount = [NSString stringWithFormat:@"%@", loan.loan_amount];
        }
        
        if ([loan.pay_periods isKindOfClass:[NSNumber class]]) {
            loan.pay_periods = [NSString stringWithFormat:@"%@", loan.pay_periods];
        }
        
        if ([loan.phone isKindOfClass:[NSNumber class]]) {
            loan.phone = [NSString stringWithFormat:@"%@", loan.phone];
        }
        
        if ([loan.product_id isKindOfClass:[NSNumber class]]) {
            loan.product_id = [NSString stringWithFormat:@"%@", loan.product_id];
        }
        
        if ([loan.city_id isKindOfClass:[NSNumber class]]) {
            loan.city_id = [NSString stringWithFormat:@"%@", loan.city_id];
        }
        
        if ([loan.series_id isKindOfClass:[NSNumber class]]) {
            loan.series_id = [NSString stringWithFormat:@"%@", loan.series_id];
        }
        
        if ([loan.status isKindOfClass:[NSNumber class]]) {
            loan.status = [NSString stringWithFormat:@"%@", loan.status];
        }
        
        if ([loan.supplier_id isKindOfClass:[NSNumber class]]) {
            loan.supplier_id = [NSString stringWithFormat:@"%@", loan.supplier_id];
        }
        
        [self loadTableView];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //WDLog(@"%@", error);
        
    }];
}

- (void)requestGetHis
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    
    manager.requestSerializer.timeoutInterval = 10.0f;
    
    NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:WDURLLogin];
    if([cookiesdata length]) {
        NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
        NSHTTPCookie *cookie;
        for (cookie in cookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }
    }
    
    NSDictionary *parameters = @{
                                 @"finance_id":self.financeID,
                                 @"his_type":@2,
                                 @"query_type":@1,
                                 @"ctype":@3
                                 };
    
    NSString *url = [NSString stringWithFormat:@"%@%@", WDURLPrefix, WDURLFinanceGetHis];
    
    [manager POST:url parameters:parameters constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.checkArray = [WDCheck mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //WDLog(@"%@", error);
        
    }];
}

#pragma mark - <tableView代理>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.checkArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WDCheck *check = self.checkArray[indexPath.row];
    
    WDCheckCell *cell = [WDCheckCell checkCellWithTableView:tableView];
    
    cell.delegate = self;
    
    [cell setDataWithModel:check index:indexPath.row maxIndex:self.checkArray.count - 1];
    
    self.imagesArray = [WDMaterialList mj_objectArrayWithKeyValuesArray:check.material_list];
    cell.imagesArr = self.imagesArray;
    
    cell.check = check;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WDCheck *check = self.checkArray[indexPath.row];
    return check.checkCellH;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WDCheckHeadView *headView = [WDCheckHeadView checkHeadView];
    headView.loan = self.loan;
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 130;
}

#pragma mark - <checkCellDelegate>
- (void)checkCellShowOriginalImage:(NSArray *)images index:(NSInteger)index
{
    WDOriImageController *tempVC = [[WDOriImageController alloc] init];
    
    tempVC.images = images;
    tempVC.index = index;
    [self.navigationController pushViewController:tempVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
